package com.yuexun.dhdoctor.module.email.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.email.adapter.MailFileAdapter;
import com.yuexun.dhdoctor.module.email.bean.MailDataBean;
import com.yuexun.dhdoctor.module.email.bean.MailFileBean;
import com.yuexun.dhdoctor.module.email.bean.MailFileDataBean;
import com.yuexun.dhdoctor.module.email.bean.SendBean;
import com.yuexun.dhdoctor.module.email.model.impl.InBoxDetailModel;
import com.yuexun.dhdoctor.module.email.presenter.impl.InBoxDetailPresenter;
import com.yuexun.dhdoctor.module.email.ui.view.IInBoxView;
import com.yuexun.dhdoctor.util.URLImageGetter;
import com.yuexun.dhdoctor.widget.ActionSheetDialog;
import com.yuexun.dhdoctor.widget.FixedSwipeListView;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/8/2.
 */
public class Act_InBoxDetail extends BaseKJActivity implements IInBoxView {

    @Bind(R.id.inbox_title)
    TextView inboxTitle;
    @Bind(R.id.inbox_sender)
    TextView inboxSender;
    @Bind(R.id.inbox_geter)
    TextView inboxGeter;
    @Bind(R.id.inbox_time)
    TextView inboxTime;
    @Bind(R.id.inbox_content)
    TextView inboxContent;
    @Bind(R.id.inbox_file_list)
    FixedSwipeListView inboxFileList;
    @Bind(R.id.inboxdetail_bottom_ll)
    RelativeLayout inboxdetailBottomLl;
    @Bind(R.id.adjunct_view)
    LinearLayout adjunctView;

    private ArrayList<MailDataBean> mailDataBeans = new ArrayList<>();
    private int positon = -1;
    private ArrayList<MailFileDataBean> mailFileDataBeans = new ArrayList<>();

    private InBoxDetailPresenter presenter;

    private MailFileAdapter adapter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_inboxdetail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        mailDataBeans = (ArrayList<MailDataBean>) getIntent().getExtras().getSerializable("maillist");
        positon = getIntent().getExtras().getInt("position");
        presenter = new InBoxDetailPresenter(new InBoxDetailModel(), this);
        readMail();
        setData();
    }

    private void setData() {
        inboxTitle.setText(mailDataBeans.get(positon).getTitle());
        inboxSender.setText(mailDataBeans.get(positon).getAddresser());
        inboxGeter.setText(mailDataBeans.get(positon).getRecipients());
        inboxTime.setText(mailDataBeans.get(positon).getUpdate_DT());
        inboxContent.setMovementMethod(LinkMovementMethod.getInstance());
//        inboxContent.setText(Html.fromHtml(mailDataBeans.get(positon).getContent().replace("＜", "<").replace("＞",">")));
        inboxContent.setText(Html.fromHtml(mailDataBeans.get(positon).getContent().replace("＜", "<").replace("＞", ">").replace(AppConfig.TEXTVIEW_WEB, AppConfig.TEXTVIEW_WEB_All), new URLImageGetter(inboxContent), null));

        if (mailDataBeans.get(positon).getFileURL() != null && !mailDataBeans.get(positon).getFileURL().isEmpty()) {
            adjunctView.setVisibility(View.VISIBLE);
            String[] file = mailDataBeans.get(positon).getFileURL().split(";");
            for (int i = 0; i < file.length; i++) {
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("fileUuid", file[i]);
                presenter.getFileData(params);
            }
        } else {
            adjunctView.setVisibility(View.GONE);
        }

    }

    private void readMail() {
        KJHttp kjHttp = new KJHttp();
        String url = AppConfig.WEB_SYSTEM + "getMailDetail.json?sessionUuid="
                + PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID)
                + "&&mailID=" + mailDataBeans.get(positon).getMailID();
        kjHttp.get(url, new HttpCallBack() {
            @Override
            public void onSuccess(Map<String, String> headers, byte[] t) {
                super.onSuccess(headers, t);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
            }
        });
    }


    @OnClick({R.id.back, R.id.inbox_reply, R.id.inbox_transmit, R.id.inbox_delete, R.id.inbox_back, R.id.inbox_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.inbox_reply:
                selectDialog();
                break;
            case R.id.inbox_transmit:
                Bundle bundle_transmit = new Bundle();
                Intent intent_transmit = new Intent(aty, Act_TransmitMail.class);
                bundle_transmit.putSerializable("maillist", mailDataBeans.get(positon));
                bundle_transmit.putSerializable("filelist", mailFileDataBeans);
                intent_transmit.putExtras(bundle_transmit);
                startActivity(intent_transmit);
                break;
            case R.id.inbox_delete:
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("mailID", mailDataBeans.get(positon).getMailID());
                presenter.deleteMail(params);
                break;
            case R.id.inbox_back:
                if (positon != 0) {
                    positon--;
                    if (adapter != null && mailFileDataBeans.size() != 0) {
                        mailFileDataBeans.clear();
                        adapter.notifyDataSetChanged();
                    }
                    readMail();
                    setData();
                } else {
                    ViewInject.toast("已到顶");
                }
                break;
            case R.id.inbox_next:
                if (positon != (mailDataBeans.size() - 1)) {
                    positon++;
                    if (adapter != null && mailFileDataBeans.size() != 0) {
                        mailFileDataBeans.clear();
                        adapter.notifyDataSetChanged();
                    }
                    readMail();
                    setData();
                } else {
                    ViewInject.toast("已到顶");
                }
                break;
        }
    }

    @Override
    public void getFileData(MailFileBean mailFileBean) {
        if (mailFileBean.getStatus().equals("200")) {
            mailFileDataBeans.add(mailFileBean.getDatas().get(0));
            adapter = new MailFileAdapter(this, mailFileDataBeans, false);
            inboxFileList.setAdapter(adapter);
        } else {
            Log.e("jx", "文件信息获取失败");
        }
    }

    @Override
    public void deleteMail(SendBean sendBean) {
        if (sendBean.getStatus().equals("200")) {
            if (positon == (mailDataBeans.size() - 1) && mailDataBeans.size() != 1) {
                mailDataBeans.remove(positon);
                positon--;
                mailFileDataBeans.clear();
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                readMail();
                setData();
            } else if (positon == 0 && mailDataBeans.size() == 1) {
                ViewInject.toast("已删除全部邮件");
                finish();
            } else {
                mailDataBeans.remove(positon);
                mailFileDataBeans.clear();
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                readMail();
                setData();
            }
        } else {
            ViewInject.toast("删除邮件失败");
        }
    }

    //选择回复、回复全部
    private void selectDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("回复", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                Bundle bundle_reply = new Bundle();
                                Intent intent_reply = new Intent(aty, Act_ReplyMail.class);
                                bundle_reply.putSerializable("maillist", mailDataBeans.get(positon));
                                bundle_reply.putSerializable("filelist", mailFileDataBeans);
                                bundle_reply.putInt("all", 0);
                                intent_reply.putExtras(bundle_reply);
                                startActivity(intent_reply);
                            }
                        })
                .addSheetItem("回复全部", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                Bundle bundle_reply = new Bundle();
                                Intent intent_reply = new Intent(aty, Act_ReplyMail.class);
                                bundle_reply.putSerializable("maillist", mailDataBeans.get(positon));
                                bundle_reply.putSerializable("filelist", mailFileDataBeans);
                                bundle_reply.putInt("all", 1);
                                intent_reply.putExtras(bundle_reply);
                                startActivity(intent_reply);
                            }
                        });
        actionSheetDialog.show();
    }
}
